package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.han.bom.Exam;
import com.han.bom.PracticeExam;
import com.han.bom.Professor;
import com.han.bom.RealExam;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.QuestionDAO;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class ProfessorHomeServlet
 */
@WebServlet("/Professor/ProfessorHomeServlet")
public class ProfessorHomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfessorHomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		ArrayList<String> messages = (ArrayList<String>) session.getAttribute("messages");
		Professor professor = (Professor) session.getAttribute("professor");
		
		if(messages==null){
			
			messages = new ArrayList<String>();
			session.setAttribute("messages",messages);
			
		}
		
		if(professor==null){
//			error
//			System.out.println("what");
		}
		
		UserDAO userdao = new UserDAO();
		QuestionDAO questiondao = new QuestionDAO();
		ExamDAO examdao = new ExamDAO();

		
		try {
			int studentCount = userdao.retrieveStudentsByProfessor(professor.getUserID()).size();
			int questionCount = questiondao.retrieveAllQuestions().size();
			HashMap<RealExam,Double> examAverageMap = examdao.retrieveRealExamsResults(professor.getUserID());
			ArrayList<Exam> realExams = examdao.retrieveExamsByProfessor(professor.getUserID());
			ArrayList<Exam> practiceExams = new ArrayList<Exam>();
			
			for(Exam exam:realExams){
				exam.setQuestions(questiondao.retrieveQuestionsbyExam(exam.getExamID()));
			}
			
			for(int i=0;i<realExams.size();i++){
				if(realExams.get(i) instanceof PracticeExam){
					practiceExams.add(realExams.remove(i));
					i--;
				}
			}
			
			ArrayList<Double> averages = new ArrayList<Double>();
			
			for(int i=0;i<realExams.size();i++){
				averages.add(examAverageMap.get(realExams.get(i)));
			}
			
			
			
			ArrayList<Exam> recentRealExams = new ArrayList<Exam>();
			ArrayList<Exam> recentPracticeExams = new ArrayList<Exam>();
			
			for(int i=1;i<=8;i++){
				if(realExams.size()-i>=0){
					recentRealExams.add(realExams.get(realExams.size()-i));
				}else{
					break;
				}
			}
			
			for(int i=1;i<=8;i++){
				if(practiceExams.size()-i>=0){
					recentPracticeExams.add(practiceExams.get(practiceExams.size()-i));
				}else{
					break;
				}
			}
			
//			System.out.println(realExams);
//			System.out.println(practiceExams);
//			System.out.println(recentRealExams);
//			System.out.println(recentPracticeExams);
			
			request.setAttribute("studentCount", studentCount);
			request.setAttribute("questionCount", questionCount);
			request.setAttribute("averages", averages);
			request.setAttribute("realExams", realExams);
			request.setAttribute("practiceExams", practiceExams);
			request.setAttribute("recentRealExams", recentRealExams);
			request.setAttribute("recentPracticeExams", recentPracticeExams);
			
			RequestDispatcher rd = request.getRequestDispatcher("Home.jsp");
			rd.forward(request, response);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}

}
